﻿using Modules.Corp;
using Modules.Person;
using Modules.Project;
using Modules.Project.Entitys;
using Modules.System.Security;
using PmSoft;
using PmSoft.Utilities;
using static Modules.Person.PersonConstants;

namespace AppWebApi.Models.Dtos
{
    public class MyGroupDto
    {
        /// <summary>
        /// 项目名称
        /// </summary>
        public string ProjectName { get; set; }
        /// <summary>
        /// 所属公司
        /// </summary>
        public string CorpName { get; set; }
        /// <summary>
        /// 班长名称
        /// </summary>
        public string LeaderName { get; set; }
        /// <summary>
        /// 人员当前状态
        /// </summary>
        public string StatusName { get; set; }
        public 进出场状态 Status { get; set; }
        public string GroupName { get; set; }
        public string? WorkType {  get; set; }
        public int? ProjectGroupMemberId {  get; set; }
    }

    public static partial class UserEmployeeExceptions
    {
        public static IEnumerable<MyGroupDto> AsMyGroups(this IEnumerable<ProjectGroupMember> members)
        {

            List<MyGroupDto> dtos = new();
            var projectGroupService = ServiceAccessor.GetService<ProjectGroupService>();
            var projectService = ServiceAccessor.GetService<ProjectService>();
            var personService = ServiceAccessor.GetService<PersonService>();
            var corpService = ServiceAccessor.GetService<CorpService>();
            var groups = projectGroupService.GetProjectGroups(members.Select(a => a.GroupId));
            var corps = corpService.GetCorpInfos(members.Select(a => a.CorpId));
            var projects = projectService.GetProjectInfoByProjectIds(members.Select(a => a.ProjectId));
            var projectGroupMemberService = ServiceAccessor.GetService<ProjectGroupMemberService>();
            var groupMembers = projectGroupMemberService.GetProjectGroupMembersByGroupIds(groups.Select(a => a.GroupId));
            foreach (var item in members)
            {
                MyGroupDto dto = new MyGroupDto();
                var project = projects.FirstOrDefault(a => a.ProjectId == item.ProjectId);
                if (project != null)
                {
                    dto.ProjectName = project.Name ?? "";
                }
                dto.CorpName = corps.FirstOrDefault(a => a.CorpId == item.CorpId)?.Name ?? "";
                var leader = groupMembers.FirstOrDefault(a => a.GroupId == item.GroupId && a.IsLeader == true);
                if (leader != null)
                {
                    dto.LeaderName = personService.GetPersonsByPersonIds(ValueUtility.ConvertToList<int>(leader.PersonId.ToString())).FirstOrDefault()?.Name ?? "";
                }
                dto.StatusName = item.Status.GetDescription();
                dto.Status = item.Status;
                dto.GroupName = groups.FirstOrDefault(a => a.GroupId == item.GroupId)?.Name ?? "";
                dto.WorkType = item.Type?.GetDescription()?? item.PostCategory?.GetDescription()??"";
                dto.ProjectGroupMemberId = item.Id;
                dtos.Add(dto);
            }
            return dtos;
        }

        public static MyGroupDto AsMyGroup(this ProjectGroupMember member)
        {

            List<MyGroupDto> dtos = new();
            var projectGroupService = ServiceAccessor.GetService<ProjectGroupService>();
            var projectService = ServiceAccessor.GetService<ProjectService>();
            var personService = ServiceAccessor.GetService<PersonService>();
            var corpService = ServiceAccessor.GetService<CorpService>();
            var group = projectGroupService.GetProjectGroup(member.GroupId);
            var corps = corpService.GetCorpInfo(member.CorpId);
            var project = projectService.GetProjectInfo(member.ProjectId);
            var projectGroupMemberService = ServiceAccessor.GetService<ProjectGroupMemberService>();
            var groupMembers = projectGroupMemberService.GetProjectGroupMembersByGroupId(group?.GroupId??0);
            MyGroupDto dto = new MyGroupDto();
            if (project != null)
            {
                dto.ProjectName = project.Name ?? "";
            }
            var leader = groupMembers.FirstOrDefault(a => a.GroupId == member.GroupId && a.IsLeader == true);
            if (leader != null)
            {
                dto.LeaderName = personService.GetPersonsByPersonIds(ValueUtility.ConvertToList<int>(leader.PersonId.ToString())).FirstOrDefault()?.Name ?? "";
            }
            dto.StatusName = member.Status.GetDescription();
            dto.Status = member.Status;
            dto.GroupName = group?.Name ?? "";
            dto.WorkType = member.Type?.GetDescription() ?? member.PostCategory?.GetDescription() ?? "";
            dto.ProjectGroupMemberId = member.Id;
            return dto;
        }
    }
}
